package com.gizwits.opensource.appkit.yasystem;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;
import com.gizwits.opensource.appkit.ControlModule.GosControlModuleBaseActivity;
import com.gizwits.opensource.appkit.ControlModule.GosDeviceControlActivity;
import com.gizwits.opensource.appkit.R;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentHashMap;

public class Main1Activity extends GosControlModuleBaseActivity {
    Button button1,button2,button3,button4,button5;
    private LocalBroadcastManager localBroadcastManager;
    int i =0;
    String data;
    String tempdata;
    private int color=0;
    private String Stone = "";
    private GizWifiDevice device;
    GosControlModuleBaseActivity go;
    TextView textView;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        button1 = (Button) findViewById(R.id.btn2);
        button2 = (Button) findViewById(R.id.btn3);
        imageView = (ImageView) findViewById(R.id.image);
        textView = (TextView) findViewById(R.id.tv_in);
        go = new GosControlModuleBaseActivity();

        Intent intent = getIntent();
        if(intent!=null) {
            Stone = intent.getStringExtra("Stone");
            device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
            device.setListener(mGizWifiDeviceListener);
            new Thread(new My1Thread()).start();
        }else{
            Toast.makeText(Main1Activity.this,"网络可能出现了故障",Toast.LENGTH_SHORT).show();
        }
        button3 = (Button) findViewById(R.id.btn4);
        button4 = (Button) findViewById(R.id.btn5);
        button5 = (Button) findViewById(R.id.btn6);
        button5.setOnClickListener(onClickListener);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        imageView.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
        button4.setOnClickListener(onClickListener);
        if (Stone.equals("true")){
            color(1);
        }

    }

    Button.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn2:
                    color =1;
                    new Thread(new My1Thread()).start();
                    data = "SaustartE";
                    retun(data);
                    color(color);
                    break;
                case R.id.btn3:
                        color=2;
                        data = "SaustopE";
                        retun(data);
                        color(color);
                    break;
                case R.id.btn4:
                    data="SaurestE";
                    retun(data);
                    break;
                case R.id.btn6:
                    color =3;
                    data = "SonlystartE";
                    retun(data);
                    color(color);
                    break;
                case R.id.btn5:
                    data = "ScloseautoE";
                    retun(data);
                    finish();
                    break;
                case R.id.image:
                    data = "ScloseautoE";
                    retun(data);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };



    ///按钮颜色改变
    private  void color(int i){
        switch (i){
            case 1:
                button1.setBackgroundColor(Color.parseColor("#FF9500"));
                button5.setBackgroundColor(Color.parseColor("#F8DC26"));
                button2.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 2:
                button1.setBackgroundColor(Color.parseColor("#F8DC26"));
                button5.setBackgroundColor(Color.parseColor("#F8DC26"));
                button2.setBackgroundColor(Color.parseColor("#FF9500"));
                break;
            case 3:
                button1.setBackgroundColor(Color.parseColor("#F8DC26"));
                button5.setBackgroundColor(Color.parseColor("#FF9500"));
                button2.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
        }
    }

    ///自动跳转
    public class My1Thread implements Runnable {
        @Override
        public void run() {
            Boolean s = true;
            // TODO Auto-generated method stub
            while (s) {
                if (data_hetime==0) {
                    handler.sendEmptyMessage(100);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (i==0) {
                        Intent intent1 = new Intent(Main1Activity.this, Secend1Activity.class);
                        intent1.putExtra("Stone", Stone);
                        Bundle bundle1 = new Bundle();
                        bundle1.putParcelable("GizWifiDevice", device);
                        intent1.putExtras(bundle1);
                        startActivity(intent1);
                    }
                    break;
                }
            }
        }
    }


    //弹窗
    Handler handler = new Handler()
    {
        public void handleMessage(Message paramMessage)
        {
            final AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(Main1Activity.this);
            normalDialog.setIcon(R.drawable.back);
            normalDialog.setTitle("提示窗口");
            normalDialog.setMessage("和面完成，若无操作五秒后将跳转压面界面?");
            normalDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            i=0;
                        }
                    });
            normalDialog.setNegativeButton("取消",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            i=1;
                        }
                    });
            // 显示
            normalDialog.show();
        }
    };

//本地广播
    public void retun(String data){
        Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
        intent.putExtra("key",data);
        intent.putExtra("data",true);
        localBroadcastManager.sendBroadcast(intent);
    }


    private GizWifiDeviceListener mGizWifiDeviceListener= new GizWifiDeviceListener(){
//接收数据回调
        @Override
        public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int sn) {
            super.didReceiveData(result, device, dataMap, sn);
            Log.d("接收到的数据",dataMap.toString());
            go.getDataFromReceiveDataMap(dataMap);
            updateUI();
        }
    };

    ///更新界面
    protected void updateUI() {
        tempdata = "";
        if (data_fire){
            show(this);
        }
        tempdata+="剩余和面时间"+data_hetime+"  ";
        textView.setText(tempdata);
    }
    @Override
    protected void onDestroy() {
        data = "ScloseautoE";
        retun(data);
        i=1;
        super.onDestroy();
    }

    ///显示消息
    public void show(Context context){
        Bitmap btm = BitmapFactory.decodeResource(getResources(),
                R.drawable.back);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.back)
                .setContentTitle("压面机系统")
                .setContentText("有火灾发生!!");
        mBuilder.setTicker("您有一条新消息");//第一次提示消息的时候显示在通知栏上
        mBuilder.setNumber(12);
        mBuilder.setLargeIcon(btm);
        mBuilder.setAutoCancel(true);//自己维护通知的消失

//获取通知管理器对象
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }

}
